package week2;



public class SeqList<T> implements List<T> {
    private static Object[] elements;
    private static int length;

    public SeqList() {
        this.elements = new Object[10];
        this.length = 0;
    }

    public SeqList(T[] data) {
        this.elements = new Object[data.length];
        for(int i = 0; i < data.length; i++) {
            this.elements[i] = data[i];
        }

        this.length = data.length;
    }

    public static void main(String[] args) {
        System.out.println("--打印线性表--");
        Integer[] data = {6, 23, 18, 90, 78, 1, 2, 4, 8,9};
        SeqList<Integer> seqList = new SeqList<Integer>(data);
        System.out.println(seqList);
        System.out.println("--查找第3个元素--");
        System.out.println(seqList.search(3));
        System.out.println("--删除第10个元素--");
        seqList.remove(10);
        System.out.println(seqList);
        System.out.println("--在第二个元素前，插入元素66--");
        seqList.insert(2,66);
        System.out.println(seqList);
        System.out.println("--求所有的元素和--");
        System.out.println(seqList.Sum());
        System.out.println("--最大的数和最小的数--");
        System.out.println(seqList.getMax());
        System.out.println(seqList.getMin());
        System.out.println(seqList);
        System.out.println("--逆置后的数据--");
        seqList.reverse( seqList.getElements(),10);
        System.out.println(seqList);
        System.out.println();



    }


    @Override
    public String toString() {
            String s = "";
            for (int i = 0; i < this.length; i++) {
                s += this.elements[i] + " ";
            }

            if(s.length() == 0) {
                return s;
            }
            return s.substring(0,s.length() - 1);

    }

    @Override
    public boolean isEmpty() {
        if(this.length == 0) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public int size() {
        return this.length;
    }

    @Override
    public T get(int i) {
        return (T) this.elements[i];
    }

    @Override
    public void set(int i, T x) {
        this.elements[i] = x;
    }

    public Object[] getElements() {
        return elements;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    @Override
    public int insert(int i, T x) {


        if(i < 1 || i > this.length+1)      //判断输入的位置是否合法
            return -1;
         if(this.length == this.elements.length)         //判断顺序表是否已满
            return -2;


        for (int j = this.length-1; j >= i-1 ; j--) {//     后移
            this.elements[j+1] = this.elements[j];
        }
        this.elements[i-1] = x;
        this.length++;

        return 0;
    }

    @Override
    public int insert(T x) {

        this.elements[length++] = x;
        return 0;
    }

    @Override
    public T remove(int i) {
        if(i < 1 || i > this.length) {
            return null;
        }else if(i == this.length) {
            this.length--;
            return null;
        }else {
            for (int j = i - 1; j < this.length; j++) {  //从要删除元素的位置开始，将后面的元素逐个向前移动一位，填补删除元素后的空缺
                this.elements[j] = this.elements[j + 1];
            }
            this.length--;
            return  null;
        }
    }

    @Override
    public void clear() {

    }

    @Override
    public int search(T key) {
        int i = (Integer) key - 1;
        return (int) this.elements[i];
    }

    @Override
    public T removeByKey(T key) {
        if((Integer) key == this.length){
            this.length--;
        }else{
            for (int i = (Integer)key - 1; i < this.length; i++) {
                this.elements[i] = this.elements[i + 1];
            }
            this.length--;
        }
        return key;
    }

    public int Sum(){       //求和
        int sum = 0;
        for (int i = 0; i < this.length; i++) {
            sum += (int) this.elements[i];
        }
        return sum;
    }

    public String getMax(){     //最大值
        int t = Integer.MIN_VALUE;
        for (int i = 0; i < this.length; i++){
            if((int)this.elements[i] > t){
                t = (int) this.elements[i];
            }
        }
        return "最大值：" + t;
    }

    public String getMin(){      //最小值
        int t = Integer.MAX_VALUE;
        for (int i = 0; i < this.length; i++){
            if((int)this.elements[i] < t){
                t = (int) this.elements[i];
            }
        }
        return "最小值：" + t;
    }

    public  void reverse(Object[] a, int n){
        int i,j;
        int temp;
        for(i = 0,j = n-1; i < j; i++,j--){
            temp = (Integer) a[i];
            a[i] = a[j];
            a[j] = temp;
        }


       // return "逆置后的数据" + this.toString();
    }

}
